// 小程序接口

/**
 * 小程序权限
 * @param {String} scope 权限
 * @param {String} name 权限名称
 * @returns then 授权成功 catch 授权失败
 */
function authorize(scope, name) {
	return new Promise((resolve, reject) => {

		uni.getSetting({
			success: (res) => {
				console.log(res.authSetting);
				if (res.authSetting && res.authSetting[scope]) {
					// 已授权
					console.log(res.authSetting[scope]);
					resolve({
						scope: scope,
						name: name
					});
				} else if (!res.authSetting.hasOwnProperty(scope)) {
					// 一次授权都没有
					uni.authorize({
						scope: scope,
						success() {
							resolve({
								scope: scope,
								name: name
							});
						},
						fail() {
							uni.showModal({
								title: '温馨提示',
								content: `您未同意"${name}"权限！`,
								showCancel: true,
								cancelText: '暂不授权',
								confirmText: '去授权',
								success: (mRes) => {
									if (mRes.confirm) {
										uni.openSetting({
											success: (setRes) => {
												if (setRes
													.authSetting &&
													setRes
													.authSetting[
														scope]) {
													resolve({
														scope: scope,
														name: name
													});
												} else {
													reject(new Error(
														'您拒绝了授权！'
													));
												}
											},
											fail: () => {
												reject(new Error(
													'打开权限设置失败！'
												));
											}
										});
									} else if (mRes.cancel) {
										reject(new Error('您拒绝了授权！'));
									}
								},
								fail: () => {
									reject(new Error('权限弹窗提示未弹出！'));
								}
							});
						}
					})
				} else {
					uni.showModal({
						title: '温馨提示',
						content: `您未同意"${name}"权限！`,
						showCancel: true,
						cancelText: '暂不授权',
						confirmText: '去授权',
						success: (mRes) => {
							if (mRes.confirm) {
								uni.openSetting({
									success: (setRes) => {
										if (setRes.authSetting && setRes
											.authSetting[
												scope]) {
											resolve({
												scope: scope,
												name: name
											});
										} else {
											reject(new Error('您拒绝了授权！'));
										}
									},
									fail: () => {
										reject(new Error('打开权限设置失败！'));
									}
								});
							} else if (mRes.cancel) {
								reject(new Error('您拒绝了授权！'));
							}
						},
						fail: () => {
							reject(new Error('权限弹窗提示未弹出！'));
						}
					});
				}
			},
			fail: (err) => {
				console.log(err);
				reject(new Error('获取权限列表失败！'));
			}
		})

	});
}

/**
 * 小程序登录
 * @param {String} provider 登录服务提供商
 */
function login(provider) {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: provider, //使用微信登录
			success: function(loginRes) {
				// console.log(loginRes);
				if (loginRes) {
					resolve(loginRes);
					// reject(new Error('登录失败！'));
				} else {
					reject(new Error('登录失败！'));
				}
			},
			fail: (err) => {
				reject(new Error('登录失败！'));
			}
		});
	})
}

//获取用户头像和昵称
function getUserInfo(provider) {
	return new Promise((resolve, reject) => {
		uni.getUserInfo({
			provider: provider,
			success: function(infoRes) {
				if (infoRes) {
					resolve(infoRes.userInfo);

				} else {
					reject(new Error('获取失败！'));
				}
			}
		});
	})
}

export default {
	authorize: authorize,
	login: login,
	getUserInfo: getUserInfo,
}